#include "ros/ros.h"
#include <iostream>
#include "std_msgs/String.h"

using namespace std;

int main(int argc, char **argv) {

    string nodeName = "cpppublisher";
    string topicName = "cpptopic";

    //初始化ros节点
    ros::init(argc, argv, nodeName);
    ros::NodeHandle node;

    //通过节点创建publisher
    const ros::Publisher &publisher = node.advertise<std_msgs::String>(topicName, 1000);

    //按照一定周期，发布消息
    int index = 0;
    ros::Rate rate(1);
    while (ros::ok()) {
        std_msgs::String msg;

        msg.data = "hello pub " + to_string(index);
        publisher.publish(msg);

        cout << msg.data << endl;

        rate.sleep();
        index++;
    }
    return 0;
}